package net.lab1024.sa.admin.module.workflow.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.RequiredArgsConstructor;
import net.lab1024.sa.admin.module.business.goods.domain.vo.GoodsVO;
import net.lab1024.sa.admin.module.workflow.dao.WfCategoryDao;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfCategory;
import net.lab1024.sa.admin.module.workflow.domain.form.WfCategoryBo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfCategoryVo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfFormVo;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 流程分类Service业务层处理
 *
 * @author KonBAI
 * @date 2022-01-15
 */
@RequiredArgsConstructor
@Service
public class WfCategoryService {

    @Resource
    private  WfCategoryDao wfCategoryDao;


    public WfCategory queryById(Long categoryId) {
        return wfCategoryDao.selectById(categoryId);
    }


    public PageResult<WfCategoryVo> queryPageList(WfCategoryBo category) {

        Page<?> page = SmartPageUtil.convert2PageQuery(category);

        List<WfCategoryVo> list = wfCategoryDao.queryPage(page, category);

        PageResult<WfCategoryVo> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }


    public List<WfCategoryVo> queryList(WfCategoryBo category) {

        List<WfCategoryVo> list = wfCategoryDao.queryList( category);
        return list;
    }

    private LambdaQueryWrapper<WfCategory> buildQueryWrapper(WfCategory category) {
 ;
        LambdaQueryWrapper<WfCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(category.getCategoryName()), WfCategory::getCategoryName, category.getCategoryName());
        lqw.eq(StringUtils.isNotBlank(category.getCode()), WfCategory::getCode, category.getCode());
        return lqw;
    }


    public int insertCategory(WfCategory categoryBo) {
        WfCategory add = BeanUtil.toBean(categoryBo, WfCategory.class);
        return wfCategoryDao.insert(add);
    }


    public int updateCategory(WfCategory categoryBo) {
        WfCategory update = BeanUtil.toBean(categoryBo, WfCategory.class);
        return wfCategoryDao.updateById(update);
    }


    public int deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return wfCategoryDao.deleteBatchIds(ids);
    }

    /**
     * 校验分类编码是否唯一
     *
     * @param category 流程分类
     * @return 结果
     */

    public boolean checkCategoryCodeUnique(WfCategory category) {
        boolean exist = wfCategoryDao.exists(new LambdaQueryWrapper<WfCategory>()
                .eq(WfCategory::getCode, category.getCode())
                .ne(ObjectUtil.isNotNull(category.getCategoryId()), WfCategory::getCategoryId, category.getCategoryId()));
        return !exist;
    }
}
